package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.pojo.*;
import com.gxa.insurance.service.OrderService;
import com.gxa.insurance.util.JsonResult;
import com.gxa.insurance.util.StringUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Min
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService service;
    @ApiOperation("司机端易理赔订单详情查询")
    @ApiImplicitParam(name = "id",value = "订单表主键id")
    @PostMapping("/selectDetails")
    public Map selectDetails(Integer id) throws BusinessException {
        List<Order> orders = service.selectOrderDetails(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(orders);
        return jsonResult.getMap();
    }


    @ApiOperation("根据订单编号查询订单")
    @PostMapping("/selectOrderByNumber")
    public Map selectOrderByNumber(String number) throws Exception {
        Order order = service.selectOrderByNumber(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(order);
        return jsonResult.getMap();
    }

    @ApiOperation("根据订单编号修改订单状态")
    @PostMapping("/updateOrderStatusByNumber")
    public Map updateOrderStatusByNumber(String number, String orderStatus) throws Exception {
        Integer update = service.updateOrderStatusByNumber(number, orderStatus);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }

    @ApiOperation("查询所有订单的方法")
    @ApiImplicitParam(name = "map",value = "传入  page   pageSize")
    @PostMapping("/selectAllOrder")
    public Map selectAllOrder(@RequestBody Map<String,Integer> map) throws Exception {
        Map<String, Object> orders = service.selectAllOrder(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(orders.get("list"));
        jsonResult.put("count",orders.get("count"));
        return jsonResult.getMap();
    }

    @ApiOperation("查询所有未还车的订单的方法")
    @PostMapping("/selectNotReturnedOrder")
    public Map selectNotReturnedOrder() throws Exception {
        List<Order> orders = service.selectNotReturnedOrder();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(orders);
        return jsonResult.getMap();
    }

    @GetMapping("/getType")
    @ApiOperation("获取所有订单类型")
    public Map getAllOrderType() {
        JsonResult jsonResult = new JsonResult();
        List<OrderType> orderTypes = service.selectAllType();
        if (orderTypes.size() <= 0) {
            jsonResult.setCode("0");
            jsonResult.setMsg("列表为空");
        } else {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(orderTypes);
        }
        return jsonResult.getMap();
    }

    @GetMapping("/getSource")
    @ApiOperation("获取所有订单来源")
    public Map getAllOrderSource() {
        JsonResult jsonResult = new JsonResult();
        List<String> orderSources = service.selectAllSource();
        if (orderSources.size() <= 0) {
            jsonResult.setCode("0");
            jsonResult.setMsg("列表为空");
        } else {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(orderSources);
        }
        return jsonResult.getMap();
    }


    @GetMapping("/getStatus")
    @ApiOperation("获取所有订单的状态")
    public Map getAllOrderStatus() {
        JsonResult jsonResult = new JsonResult();
        List<OrderStatus> orderStatus = service.selectAllStatus();
        if (orderStatus.size() <= 0) {
            jsonResult.setCode("0");
            jsonResult.setMsg("列表为空");
        } else {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(orderStatus);
        }
        return jsonResult.getMap();
    }

    @ApiOperation(value = "订单总表模糊查询,条件请封装成order对象传入，具体看model", produces = "body")
    @ApiImplicitParam(name = "(不传任何条件就是查询所有)查询条件字段  和分页数据  以json方式传入", value = "传值示例，请保持字段名相同 " +
            "{\n" +
            "    \"city\":\"成都\",\n" +
            "    \"name\":\"15181742723\",\n" +
            "    \"orderType\":\"理赔\",\n" +
            "    \"orderSource\":\"\",\n" +
            "    \"orderStatus\":\"\",\n" +
            "    \"createTime\":\"2022-06-07 19:33:13\",\n" +
            "    \"overTime\":\"2022-06-23 10:00:00\",\n" +
            "    \"page\":\"1\",\n" +
            "    \"pageSize\":\"10\"\n" +
            "}\n" +
            "\n" +
            "{\n" +
            "    \"city\":\"成都\",\n" +
            "    \"name\":\"15181742723\",\n" +
            "    \"orderType\":\"理赔\",\n" +
            "    \"orderSource\":\"\",\n" +
            "    \"orderStatus\":\"\",\n" +
            "    \"createTime\":null,\n" +
            "    \"overTime\":null,\n" +
            "    \"page\":\"1\",\n" +
            "    \"pageSize\":\"10\"\n" +
            "} ")
    @PostMapping("/queryLike")
    public Map selectByLike(@RequestBody Map<String, String> map) throws Exception {
        JsonResult jsonResult = new JsonResult();
        if (map.size() <= 0) {
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Map<String, Object> maps = service.selectByLike(map);
        List<Order> orders = (List<Order>) maps.get("list");
        if (orders.size() <= 0) {
            jsonResult.setCode("0");
            jsonResult.setMsg("列表为空");
        } else {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(maps.get("list"));
            jsonResult.put("count", maps.get("count"));
        }
        return jsonResult.getMap();
    }

    @ApiOperation("后台新增订单，订单编号由后端生成")
    @ApiImplicitParam(name = "map", value = "传参实例 字段名请一一对应  " +
            "{\n" +
            "\"carId\":\"川A4560\",\n" +
            "\"driver\":\"赵四喜\",\n" +
            "\"driverArea\":\"龙泉驿区\",\n" +
            "\"username\":\"张飞\",\n" +
            "\"phone\":\"136999451454\",\n" +
            "\"statusId\":\"1\",\n" +
            "\"orderStatus\":\"未支付\",\n" +
            "\"type\":\"理赔\",\n" +
            "\"typeId\":\"1\",\n" +
            "\"receivePosition\":\"尼古拉斯赵四路\",\n" +
            "\"engineNumber\":\"jh789462\",\n" +
            "\"tip\":\"注意你欠钱了\",\n" +
            "\"s4AreaId\":\"1\",\n" +
            "\"s4Area\":\"武侯区\",\n" +
            "\"inspectArea\":\"武侯区\",\n" +
            "\"s4Shop\":\"奔驰4s店\",\n" +
            "\"inspectName\":\"成都市北奔汽车修理厂销售有限公司\"\n" +
            "}")
    @PostMapping("/addByBackStage")
    public  Map  addSimpleOrderByStage(@RequestBody Map<String,String> map) throws Exception {
        if(map==null || map.size()<=0){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Boolean isOK=service.addByBackStage(map);
        if(isOK){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getMap();

    }

    @ApiOperation("批量删除的接口")
    @ApiImplicitParam(name = "map",value = "id字符串  例： \"ids\":\"1,2,3,4,5\"")
    @PostMapping("/deleteOrders")
    public Map deleteOrders(@RequestBody Map<String,String> map) throws BusinessException {
        if(map==null || StringUtil.isEmpty(map.get("ids"))){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        String ids = map.get("ids");
        JsonResult jsonResult = new JsonResult();
        Boolean isOk=service.deleteBatch(ids);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getMap();
    }


    @ApiOperation("保存易理赔订单详情")
    @ApiImplicitParam(name="请求数据json",value = "请保持字段名一致 " +
            "{\n" +
            "\"number\":\"a4fd104140924a12a798cc778721248a\",\n" +
            "\"getDriver\":\"尼古拉斯赵四\",\n" +
            "\"getDriverArea\":\"武侯区\",\n" +
            "\"returnArea\":\"武侯区\",\n" +
            "\"reDriver\":\"尼古拉斯赵四\",\n" +
            "\"orderStatus\":\"未支付\",\n" +
            "\"carId\":\"川A75699\",\n" +
            "\"s4ShopName\":\"成都晨德宝汽车销售有限公司\",\n" +
            "\"s4ShopArea\":\"武侯区\",\n" +
            "\"isFiveC\":\"true\",\n" +
            "\"workCompany\":\"碉堡了公司\",\n" +
            "\"isMakeCom\":\"true\",\n" +
            "\"isSendMess\":\"true\",\n" +
            "\"attention\":\"注意你欠钱了\",\n" +
            "\"cusMessageC\":\"注意你高利贷了\",\n" +
            "\"driMessageC\":\"注意你撞车了\",\n" +
            "\"messageOperation\":\"2\",\n" +
            "\"getCarTime\":\"2022-4-5 14:23:00\",\n" +
            "\"engineNumber\":\"8848\",\n" +
            "\"insurancedPerson\":\"尼古拉斯赵四的婆娘\",\n" +
            "\"contactPerson\":\"尼古拉斯找妞\",\n" +
            "\"contactPhone\":\"12345678978\",\n" +
            "\"damagePosition\":\"田径路\",\n" +
            "\"registerP\":\"赵天牛\",\n" +
            "\"receiveP\":\"天府新区天府三街\",\n" +
            "\"returnP\":\"龙泉北京路\",\n" +
            "\"receiveMap\":\"5466.235,54489.23\",\n" +
            "\"returnMap\":\"36644.136,64455\",\n" +
            "\"tip\":\"天后纸盒\",\n" +
            "\"completeTime\":\"2022-4-5 14:23:00\",\n" +
            "\"pictureDesc\":\"金佛IQ外耗\",\n" +
            "\"damageImg\":\"/asjflak.jsp,/kjashfa.jpg\"\n" +
            "}"
            ,dataType = "body")
    @PostMapping("/updateInsurance")
    public Map updateInsurance(@RequestBody Map<String,String>  map) throws BusinessException, ParseException {
        if(map==null || map.size()<=0 ){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.updateInsuranceByOrder(map);
        JsonResult jsonResult = new JsonResult();
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getMap();
    }


    @ApiOperation("通过 订单编号和订单类型查询跳转订单详细信息")
    @ApiImplicitParam(name = "map",value = "传入订单编号与订单类型   {'number':'412asd512qwe1551','type':'理赔'}",dataType = "body")
    @PostMapping("/selectOrderDetail")
    public Map selectOrderDetailByNumberAndType(@RequestBody Map<String,String> map) throws BusinessException {
        if(map==null || map.size()<=0 ){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Map<String,Object> datas=service.selectDetailByNumberAndType(map);
        JsonResult jsonResult = new JsonResult();
        if(datas==null || datas.size()<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.put("order",datas.get("order"));
            jsonResult.put("orderDetail",datas.get("detail"));
//            jsonResult.put("pay",datas.get("pay"));
            jsonResult.put("damageImg",datas.get("damageImg"));
            jsonResult.put("attachmentImg",datas.get("attachmentImg"));
        }
        return jsonResult.getMap();
    }

    @ApiOperation("保存验车订单详情")
    @ApiImplicitParam(name="请求数据json",value = "请保持字段名一致 " +
            "{\n" +
            "\"number\":\"eb5662c5a2a64ff48628c25d8c77ddbf\",\n" +
            "\"getDriver\":\"尼古拉斯赵四\",\n" +
            "\"reDriver\":\"尼古拉斯赵四\",\n" +
            "\"orderStatus\":\"未支付\",\n" +
            "\"workCompany\":\"尼古拉斯赵四的公司\",\n" +
            "\"carId\":\"川A75699\",\n" +
            "\"inspectPlace\":\"成都市北奔汽车修理厂销售有限公司\",\n" +
            "\"isMakeCompemsation\":\"true\",\n" +
            "\"isSendMessage\":\"true\",\n" +
            "\"attention\":\"注意你欠钱了\",\n" +
            "\"cusMessage\":\"注意你高利贷了\",\n" +
            "\"driMessage\":\"注意你撞车了\",\n" +
            "\"messageOperate\":\"2\",\n" +
            "\"getCarTime\":\"2022-4-5 14:23:00\",\n" +
            "\"engineNumber\":\"8848\",\n" +
            "\"contactPerson\":\"尼古拉斯找妞\",\n" +
            "\"contactPhone\":\"12345678978\",\n" +
            "\"registerP\":\"赵天牛\",\n" +
            "\"receiveP\":\"天府新区天府三街\",\n" +
            "\"returnP\":\"龙泉北京路\",\n" +
            "\"receiveMap\":\"5466.235,54489.23\",\n" +
            "\"returnMap\":\"36644.136,64455\",\n" +
            "\"tip\":\"天后纸盒\",\n" +
            "\"returnTime\":\"2022-4-5 14:23:00\",\n" +
            "\"pictureDesc\":\"金佛IQ外耗\",\n" +
            "\"attachmentImg\":\"/asjflak.jsp,/kjashfa.jpg\"\n" +
            "}"
            ,dataType = "body")
    @PostMapping("/updateInspect")
    public Map updateInspect(@RequestBody Map<String,String>  map) throws BusinessException, ParseException {
        if(map==null || map.size()<=0 ){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.updateInspectByOrder(map);
        JsonResult jsonResult = new JsonResult();
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("司机端通过订单编号和订单类型查询跳转订单详细信息")
    @ApiImplicitParam(name = "map",value = "传入订单编号与订单类型   {'number':'412asd512qwe1551','type':'理赔'}",dataType = "body")
    @PostMapping("/getOrderDetail")
    public Map getOrderDetailByNumberAndType(@RequestBody Map<String,String> map) throws BusinessException {
        if(map==null || map.size()<=0 ){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Map<String,Object> datas=service.selectDetailByNumberAndType(map);
        JsonResult jsonResult = new JsonResult();
        if(datas==null || datas.size()<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }else{
            Order order = (Order)datas.get("order");
            String name = order.getName();
            String phone = order.getPhone();
            InsuranceOrder detail = (InsuranceOrder)datas.get("detail");
            String returnPosition = detail.getReturnPosition();
            String receivePosition = detail.getReceivePosition();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            Date getCarTime = detail.getGetCarTime();
            String format = dateFormat.format(getCarTime);
            String shopName = detail.getS4Area().getRepairDepots().get(0).getName();
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.put("consumerName",name);
            jsonResult.put("consumerPhone",phone);
            jsonResult.put("getCarTime",format);
            jsonResult.put("receivePosition",receivePosition);
            jsonResult.put("returnPosition",returnPosition);
            jsonResult.put("shopName",shopName);

        }
        return jsonResult.getMap();
    }

}
